//
//  UserDetail_Movie.m
//  豆瓣
//
//  Created by dllo on 16/1/3.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "UserDetail_Movie.h"
#import "DataBase_Movie.h"
#import "MovieDetailVC.h"
#import "HTDataBase_DB.h"
#import "Model_DB.h"



@interface UserDetail_Movie ()
@property(nonatomic,strong) NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *data_model;

@end

@implementation UserDetail_Movie

NSString *cell_user_movie_id = @"cell_user_movie_id";

- (void)setData
{
    

    
    //电影列表接口文档:http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php
    
    self.data_model = [NSMutableArray array];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php"]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        //解析
        if (data != nil) {
            
            NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            
            
            //                NSLog(@"%@",dataDic[@"result"]);
            for (NSDictionary *dic in dataDic[@"result"]) {
                MovieListModel *model = [[MovieListModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.data_model addObject:model];
            }
            
            
            
        }
        
    }];
    
    
    
    
    [dataTask resume];
    


    
    
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self setData];
//    [[DataBase_Movie shareDataBaseHandle] openDB];
//    NSMutableArray *array = [[DataBase_Movie shareDataBaseHandle] selectmovie];
//    if (array != nil) {
//        self.dataArray = array;
//    }
//    [[DataBase_Movie shareDataBaseHandle] closeDB];
    
    self.dataArray = [[HTDataBase_DB shareData] selectAllFromTable:@"Movie"];
    
    [self.tableView reloadData];
}







- (void)viewDidLoad {
    [super viewDidLoad];

    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cell_user_movie_id];
    
    self.navigationItem.title = @"我的电影";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_user_movie_id forIndexPath:indexPath];
    
    Model_DB *model = self.dataArray[indexPath.row];
    
    cell.textLabel.text = model.title;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  
     Model_DB *model_db = self.dataArray[indexPath.row];
  
    for (MovieListModel *model__ in self.data_model) {
       
        if ([model_db.title isEqualToString:model__.movieName]) {

            MovieDetailVC *movieDetailVC = [[MovieDetailVC alloc] init];
            movieDetailVC.model = model__;
            
            [self.navigationController pushViewController:movieDetailVC animated:YES];
            
        }
    }
    


    
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //这个方法，写这不用管就OK
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        
        
        
//        MovieModel *model = self.dataArray[indexPath.row];
//        [[DataBase_Movie shareDataBaseHandle] openDB];
//        [[DataBase_Movie shareDataBaseHandle] deleteMovie:model.title];
//        [[DataBase_Movie shareDataBaseHandle] updataMovie:model];
//        [[DataBase_Movie shareDataBaseHandle] closeDB];
//        [self.dataArray removeObjectAtIndex:indexPath.row];
//        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        Model_DB *model = self.dataArray[indexPath.row];
        [[HTDataBase_DB shareData] deleteModel:model Table:@"Movie"];
        [self.dataArray removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        
        
        
    }];
    
    [self.tableView reloadData];
    
    return @[action1];
}







@end
